from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

from cardwall.greatwall.src import wall_ctrl
from cardwall.greatwall.src import stage_ctrl
from cardwall.greatwall.src import card_ctrl
from cardwall.greatwall.src import card_move_ctrl

class Views(object):

    def __init__(self):
        self.wall_ops = wall_ctrl.WallOperations()
        self.stage_ops = stage_ctrl.StageOperations()
        self.card_ops = card_ctrl.CardOperations()
        self.card_move_ops = card_move_ctrl.CardMoveOperations()

    def main_page(self, request):
        existing_walls = self.wall_ops.get_all_active_walls()
        return render_to_response('cardwall.html',
                                  {'existing_walls': existing_walls})

    def add_wall(self, request):
        title = request.POST['wall_title']
        owner = request.POST['wall_owner']
        description = request.POST['wall_desc']
        wall_obj = self.wall_ops.create_wall(title=title, owner=owner,
                                             descr=description)
        
        return HttpResponseRedirect('/cardwall/view_wall/' + str(wall_obj.id))

    def add_card(self, request):
        title = request.POST['card_title']
        start_dt = request.POST['startdate']
        end_dt = request.POST['enddate']
        devs = request.POST['card_devs']
        points = request.POST['card_points']
        color = '' #request.POST['card_color']
        desc = request.POST['card_desc']
        wall_id = request.POST['wall_id']
        stage_obj = self.wall_ops.get_starting_stage(wall_id)
        self.card_ops.create_card(title=title, stage=stage_obj, start_dt=start_dt, 
                                  end_dt=end_dt, devs=devs, desc=desc)
        return HttpResponseRedirect('/cardwall/view_wall/' + str(wall_id))

    def toogle_card(self, request, card_id):
        card = self.card_ops.get_card_by_id(card_id)        
        self.card_ops.toogle_card(card)

    def edit_card(self, request, card_id):
        card = self.card_ops.get_card_by_id(card_id)
        wall_id = card.fk_stage.fk_wall.id #fixme get_wall_id api
        return HttpResponseRedirect('/cardwall/edit_card/' + str(wall_id) + '-' + str(card.id))

    def view_wall(self, request, wall_id, edit_card_id=False):
        wall_contents = self.wall_ops.get_wall_contents(wall_id=wall_id)
        stage_width = (100.0 / len(wall_contents)) - 1
        edit_card = False
        if edit_card_id:
            edit_card = self.card_ops.get_card_by_id(edit_card_id)
        return render_to_response('view_wall.html', {'wall_contents': wall_contents,
                                                     'wall_id': wall_id,
                                                     'stage_width': stage_width,
                                                     'edit_card': edit_card})

    def move_card(self, request, card_id, new_stage_id):
        card = self.card_ops.get_card_by_id(card_id)
        new_stage = self.stage_ops.get_stage_by_id(new_stage_id)
        if card.fk_stage != new_stage:
            card.move(new_stage=new_stage)
            self.card_move_ops.save_move(card=card, new_stage=new_stage)

    def edit_stages(self, request):
        wall_id = request.POST['wall_id']
        stage_list = [request.POST['stage1'],
                      request.POST['stage2'],
                      request.POST['stage3'],
                      request.POST['stage4'],
                      request.POST['stage5'],
                      request.POST['stage6']]

        self.wall_ops.save_stages(wall_id=wall_id, 
                                  stage_list=stage_list)
        return HttpResponseRedirect('/cardwall/view_wall/' + str(wall_id))
